package com.fenfen.novel.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.provider.Settings;

public class SharedPrefsUtils {
  private static SharedPrefsUtils instance;
  private SharedPreferences sp;
  private SharedPreferences.Editor editor;

  public interface ShardPrefsKey {
    String CHAPTER_TEXT_SIZE = "chapter_text_size";
    String CHAPTER_BRIGHTNESS_MODE = "chapter_brightness_mode"; //亮度模式，随系统或者自定义
    String CHAPTER_BRIGHTNESS = "chapter_brightness"; //自定义亮度值
    String CHAPTER_NIGHT_MODE = "chapter_night_mode"; //夜间模式
    String THEME_COLOR = "theme_color"; //主题颜色
    String NAVIGATION_COLOR_MODE = "navigation_color"; //导航栏是否跟随系统
    String DOWNLOAD_DIR = "download_dir";
  }

  private SharedPrefsUtils(Context context) {
    this.sp = PreferenceManager.getDefaultSharedPreferences(context);
    this.editor = sp.edit();
  }

  public static SharedPrefsUtils getInstance(Context context) {
    if (instance == null) {
      synchronized (SharedPrefsUtils.class) {
        if (instance == null) {
          instance = new SharedPrefsUtils(context);
        }
      }
    }
    return instance;
  }

  public int getChapterTextSize() {
    return sp.getInt(ShardPrefsKey.CHAPTER_TEXT_SIZE, CommonUtil.TEXT_SIZE_DEFAULT);
  }

  public void setChapterTextSize(int textSize) {
    editor.putInt(ShardPrefsKey.CHAPTER_TEXT_SIZE, textSize);
    editor.commit();
  }

  public int getChapterBrightnessMode() {
    return sp.getInt(ShardPrefsKey.CHAPTER_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
  }

  public int getChapterBrightness() {
    return sp.getInt(ShardPrefsKey.CHAPTER_BRIGHTNESS, CommonUtil.NONE_INT_DATA);
  }

  public void setChapterBrightness(int brightness) {
    editor.putInt(ShardPrefsKey.CHAPTER_BRIGHTNESS, brightness);
    editor.apply();
  }

  public void setChapterBrightnessMode(int mode) {
    editor.putInt(ShardPrefsKey.CHAPTER_BRIGHTNESS_MODE, mode);
    editor.apply();
  }

  public boolean getChapterNightMode() {
    return sp.getBoolean(ShardPrefsKey.CHAPTER_NIGHT_MODE, false);
  }

  public void setChapterNightMode(boolean isNight) {
    editor.putBoolean(ShardPrefsKey.CHAPTER_NIGHT_MODE, isNight);
    editor.apply();
  }

  public int getThemeColor() {
    return sp.getInt(ShardPrefsKey.THEME_COLOR, 0);
  }

  public boolean getNavigationColorMode() {
    return sp.getBoolean(ShardPrefsKey.NAVIGATION_COLOR_MODE, false);
  }

  public String getDownloadDir() {
    return sp.getString(ShardPrefsKey.DOWNLOAD_DIR, CommonUtil.DOWN_DIR_DEFAULT);
  }

  public void setDownloadDir(String downDir) {
    editor.putString(ShardPrefsKey.DOWNLOAD_DIR, downDir);
    editor.apply();
  }
}
